package licode.unisop.single.user.application.service;

import cn.hutool.core.bean.BeanUtil;
import licode.unisop.provider.vo.SiPageResult;
import licode.unisop.realm.client.api.SiRealmQueryService;
import licode.unisop.realm.client.dto.SiRealmDTO;
import licode.unisop.realm.client.info.SiPlatformOfRealmQueryIn;
import licode.unisop.realm.client.info.SiRealmGrantPlatformIn;
import licode.unisop.realm.client.info.SiRealmOfPlatformQueryIn;
import licode.unisop.realm.client.info.SiRealmQueryPageIn;
import licode.unisop.single.user.application.entity.RealmPlatformGrantEntity;
import licode.unisop.single.user.application.gateway.RealmPlatformGrantDao;
import licode.unisop.single.user.domain.aggregate.entity.RealmsEntity;
import licode.unisop.single.user.domain.aggregate.repository.RealmsDao;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 领域查询接口实现
 *
 * @author licode
 */
@Component
public class SiRealmQueryServiceDefault implements SiRealmQueryService {
    @Resource
    private RealmsDao realmsDao;

    @Resource
    private RealmPlatformGrantDao realmPlatformGrantDao;

    @Override
    public List<SiRealmDTO> getRealmList() {
        List<RealmsEntity> realms = realmsDao.getRealmList();
        if (null == realms) {
            return new ArrayList<>();
        } else {
            return convert(realms);
        }
    }

    @Override
    public SiRealmDTO findRealm(String realmId) {
        RealmsEntity entity = realmsDao.getWithId(realmId);
        return entity == null ? null : BeanUtil.copyProperties(entity, SiRealmDTO.class);
    }

    @Override
    public int realmGrantToPlatform(SiRealmGrantPlatformIn req) {
        return realmPlatformGrantDao.realmGrantToPlatform(req);
    }

    @Override
    public List<String> queryPlatformOfRealmList(SiPlatformOfRealmQueryIn req) {
        List<RealmPlatformGrantEntity> result;
        result = realmPlatformGrantDao.queryByRealmId(req.getRealmId(), req.getGrantType());
        return null == result ? null : result
                .stream().map(RealmPlatformGrantEntity::getPlatformId)
                .collect(Collectors.toList());
    }

    @Override
    public List<SiRealmDTO> queryRealOfPlatformList(SiRealmOfPlatformQueryIn req) {
        List<RealmPlatformGrantEntity> result;
        result = realmPlatformGrantDao.queryByRealmId(req.getPlatformId(), req.getGrantType());
        if (null != result && !result.isEmpty()) {
            return realmsDao.queryRealmList(result
                    .stream().map(RealmPlatformGrantEntity::getRealmId)
                    .collect(Collectors.toList()));
        } else {
            return null;
        }
    }

    @Override
    public SiPageResult<SiRealmDTO> queryRealmPage(SiRealmQueryPageIn req) {
        return realmsDao.getRealmPage(req);
    }

    private List<SiRealmDTO> convert(List<RealmsEntity> realms) {
        List<SiRealmDTO> retRealms = new ArrayList<>();
        realms.forEach(item->retRealms.add(BeanUtil.copyProperties(item, SiRealmDTO.class)));
        return retRealms;
    }
}
